package org.mango.jtop.service.impl;

import com.alibaba.fastjson.JSONObject;
import io.jboot.aop.annotation.Bean;
import org.mango.jtop.service.TopService;
import org.mango.jtop.util.RemoteShellExecutor;

@Bean
public class TopServiceImpl implements TopService {

    @Override
    public JSONObject handleTopInfo() {
        RemoteShellExecutor executor = new RemoteShellExecutor("tx.meiflower.top", "root", "Mg19930324@");
        try {
            System.out.println(executor.exec("cat /proc/stat"));
            System.out.println(executor.exec("cat /proc/meminfo"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * cpu out:
     * cpu  687174 234 405826 66594757 49996 0 3628 0 0 0
     * cpu0 350967 126 205157 33295545 21535 0 1823 0 0 0
     * cpu1 336207 108 200668 33299212 28461 0 1804 0 0 0
     * intr 189024893 95 114 0 0 492 0 3 0 0 0 0 0 876 0 331884 0 0 0 0 0 0 0 0 0 0 687482 0 1228876 22 1010697 19 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     * ctxt 210411565
     * btime 1571992143
     * processes 1551315
     * procs_running 1
     * procs_blocked 0
     * softirq 85318218 1 39000012 9 3029186 166020 0 119 16992574 0 26130297
     * 4 0 0 0 0 0 0 0 0 0 0 687482 0 1228876 22 1010697 19 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     * 输出解释：
     * CPU 以及CPU0、CPU1、CPU2、CPU3每行的每个参数意思（以第一行为例）为：
     * 参数 解释
     * user (432661) 从系统启动开始累计到当前时刻，用户态的CPU时间（单位：jiffies） ，不包含 nice值为负进程。1jiffies=0.01秒
     * nice (13295) 从系统启动开始累计到当前时刻，nice值为负的进程所占用的CPU时间（单位：jiffies）
     * system (86656) 从系统启动开始累计到当前时刻，核心时间（单位：jiffies）
     * idle (422145968) 从系统启动开始累计到当前时刻，除硬盘IO等待时间以外其它等待时间（单位：jiffies）
     * iowait (171474) 从系统启动开始累计到当前时刻，硬盘IO等待时间（单位：jiffies） ，
     * irq (233) 从系统启动开始累计到当前时刻，硬中断时间（单位：jiffies）
     * softirq (5346) 从系统启动开始累计到当前时刻，软中断时间（单位：jiffies）
     *
     * CPU时间=user+system+nice+idle+iowait+irq+softirq
     *
     * “intr”这行给出中断的信息，第一个为自系统启动以来，发生的所有的中断的次数；然后每个数对应一个特定的中断自系统启动以来所发生的次数。
     * “ctxt”给出了自系统启动以来CPU发生的上下文交换的次数。
     * “btime”给出了从系统启动到现在为止的时间，单位为秒。
     * “processes (total_forks) 自系统启动以来所创建的任务的个数目。
     * “procs_running”：当前运行队列的任务的数目。
     * “procs_blocked”：当前被阻塞的任务的数目。
     *
     * 那么CPU利用率的计算方法：可以使用取两个采样点，计算其差值的办法。
     *
     * CPU利用率 = 1- (idle2-idle1)/(cpu2-cpu1)
     */

    /**
     * meminfo out:
     * MemTotal:        3881088 kB
     * MemFree:          185112 kB
     * MemAvailable:    1011012 kB
     * Buffers:          119628 kB
     * Cached:           858684 kB
     * SwapCached:            0 kB
     * Active:          2931312 kB
     * Inactive:         529040 kB
     * Active(anon):    2485924 kB
     * Inactive(anon):     9012 kB
     * Active(file):     445388 kB
     * Inactive(file):   520028 kB
     * Unevictable:           0 kB
     * Mlocked:               0 kB
     * SwapTotal:             0 kB
     * SwapFree:              0 kB
     * Dirty:               716 kB
     * Writeback:             0 kB
     * AnonPages:       2482216 kB
     * Mapped:           170600 kB
     * Shmem:             12892 kB
     * Slab:             153668 kB
     * SReclaimable:     126224 kB
     * SUnreclaim:        27444 kB
     * KernelStack:        6896 kB
     * PageTables:        22568 kB
     * NFS_Unstable:          0 kB
     * Bounce:                0 kB
     * WritebackTmp:          0 kB
     * CommitLimit:     1940544 kB
     * Committed_AS:    3429608 kB
     * VmallocTotal:   34359738367 kB
     * VmallocUsed:       14232 kB
     * VmallocChunk:   34359715580 kB
     * HardwareCorrupted:     0 kB
     * AnonHugePages:    102400 kB
     * CmaTotal:              0 kB
     * CmaFree:               0 kB
     * HugePages_Total:       0
     * HugePages_Free:        0
     * HugePages_Rsvd:        0
     * HugePages_Surp:        0
     * Hugepagesize:       2048 kB
     * DirectMap4k:       90104 kB
     * DirectMap2M:     4104192 kB
     * (anon):    2485924 kB
     * Inactive(anon):     9012 kB
     * Active(file):     445388 kB
     * Inactive(file):   520028 kB
     * Unevictable:           0 kB
     * Mlocked:               0 kB
     * SwapTotal:             0 kB
     * SwapFree:              0 kB
     * Dirty:               716 kB
     * Writeback:             0 kB
     * AnonPages:       2482216 kB
     * Mapped:           170600 kB
     * Shmem:             12892 kB
     * Slab:             153668 kB
     * SReclaimable:     126224 kB
     * SUnreclaim:        27444 kB
     * KernelStack:        6896 kB
     * PageTables:        22568 kB
     * NFS_Unstable:          0 kB
     * Bounce:                0 kB
     * WritebackTmp:          0 kB
     * CommitLimit:     1940544 kB
     * Committed_AS:    3429608 kB
     * VmallocTotal:   34359738367 kB
     * VmallocUsed:       14232 kB
     * VmallocChunk:   34359715580 kB
     * HardwareCorrupted:     0 kB
     * AnonHugePages:    102400 kB
     * CmaTotal:
     *
     * 内存使用率 = 1 - MemFree/MemTotal
     */
}
